package com.example.touch;

import java.util.ArrayList;
import java.util.List;

import android.util.Log;

import com.example.touch.wifipositioning.Parser;
import com.example.touch.wifipositioning.PositionCalculationResult;
import com.example.touch.wifipositioning.TraceEntry;
import com.example.touch.wifipositioning.WifiPositioner;

public class WifiTester {
	
	private Parser parser = null;

	public void testOnlineData()
	{
		Log.d("WifiTester", "Test started");
        parser = new Parser("/IndoorPositioning/Online1");
	}
	
	public void testOverfitData()
	{
        parser = new Parser("/IndoorPositioning/OnlineOverfit");
	}
	
	public void startTestNN()
	{
		if(parser != null)
		{
			WifiPositioner wifiPositioner = new WifiPositioner();
	        ArrayList<TraceEntry> onlineTraceEntries = parser.parse();
	        int totalError = 0;
	        for(TraceEntry onlineTrace : onlineTraceEntries)
	        {
	        	PositionCalculationResult positionResultNN = wifiPositioner.fingerPrintNN(onlineTrace);
	        	if(positionResultNN != null)
	        	{
	        		Log.d("WifiTester", "Position result NN: " + positionResultNN.toString());
	        		totalError += positionResultNN.getReal().distance(positionResultNN.getEstimated()); 
	        	}
	        }
	        int avg = totalError / onlineTraceEntries.size();
	        Log.d("WifiTester", "Average error: " + avg);
		}
	}
	
	public void startTestKNN(int k)
	{
		if(parser != null)
		{
			WifiPositioner wifiPositioner = new WifiPositioner();
	        ArrayList<TraceEntry> onlineTraceEntries = parser.parse();
	        int totalError = 0;
	        for(TraceEntry onlineTrace : onlineTraceEntries)
	        {
	        	PositionCalculationResult positionResultKNN = wifiPositioner.fingerPrintKNN(k, onlineTrace);
	        	if(positionResultKNN != null)
	        	{
	        		Log.d("WifiTester", "Position result KNN: " + positionResultKNN.toString());
	        		totalError += positionResultKNN.getReal().distance(positionResultKNN.getEstimated()); 
	        	}
	        }
	        int avg = totalError / onlineTraceEntries.size();
	        Log.d("WifiTester", "Average error: " + avg);
		}
	}
	
	public void startTestKNNWeighted(int k)
	{
		if(parser != null)
		{
			WifiPositioner wifiPositioner = new WifiPositioner();
	        ArrayList<TraceEntry> onlineTraceEntries = parser.parse();
	        List<Double> bestDistance = new ArrayList<Double>();
	        int totalError = 0;
	        for(TraceEntry onlineTrace : onlineTraceEntries)
	        {
	        	PositionCalculationResult positionResultKNNWeighted = wifiPositioner.fingerPrintKNNWeighted(k, onlineTrace);
	        	if(positionResultKNNWeighted != null)
	        	{
	        		double distance = positionResultKNNWeighted.getReal().distance(positionResultKNNWeighted.getEstimated());
	        		
	        		if(bestDistance.size() < 10) {
	        			bestDistance.add(distance);
	        		} else {
	        			int counter = 0;
	        			double maxValue = 0;
	        			
	        			for(int i=0; i<bestDistance.size(); i++)
	        			{
	        				if(bestDistance.get(i) > maxValue)
	        				{
	        					maxValue = bestDistance.get(i);
	        					counter = i;
	        				}	        				
	        			}
	        			if(distance < maxValue)
	        			{
	        				bestDistance.remove(counter);
	        				bestDistance.add(distance);
	        			}
	        		}
	        		
	        		Log.d("WifiTester", "Position result KNNW: " + positionResultKNNWeighted.toString());
	        		totalError += positionResultKNNWeighted.getReal().distance(positionResultKNNWeighted.getEstimated()); 
	        	}
	        }
	        int avg = totalError / onlineTraceEntries.size();
	        Log.d("WifiTester", "Average error: " + avg);
	        double average = 0;
	        for(double value : bestDistance)
	        {
	        	average += value;
	        }
	        average = average / bestDistance.size();
	        Log.d("WifiTester", "Adjusted average error: " + average);
		}
		
	}
}
